package com.anyou.mall.fin.service.impl;

import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.fin.mapper.OrderAmountMapper;
import com.anyou.mall.fin.mapper.StoreWithdrawalAduitMapper;
import com.anyou.mall.fin.param.RevenueParam;
import com.anyou.mall.fin.param.TixRecordParam;
import com.anyou.mall.fin.vo.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.fin.mapper.StoreReconciliationMapper;
import com.anyou.mall.fin.domain.StoreReconciliation;
import com.anyou.mall.fin.service.IStoreReconciliationService;

/**
 * 商家对账Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-04-25
 */
@Service
public class StoreReconciliationServiceImpl extends ServiceImpl<StoreReconciliationMapper,StoreReconciliation> implements IStoreReconciliationService
{
    @Autowired
    private StoreReconciliationMapper storeReconciliationMapper;

    @Autowired
    private StoreWithdrawalAduitMapper storeWithdrawalAduitMapper;

    @Autowired
    private OrderAmountMapper orderAmountMapper;

    /**
     * 查询商家对账
     *
     * @param storeReconciliationId 商家对账主键
     * @return 商家对账
     */
    @Override
    public StoreReconciliationDetails selectStoreReconciliationByStoreReconciliationId(Long storeReconciliationId)
    {
        StoreReconciliationDetails storeReconciliationDetails = new StoreReconciliationDetails();
        StoreReconciliationVo storeReconciliationVo = storeReconciliationMapper.selectStoreReconciliationByStoreReconciliationId(storeReconciliationId);
        if (storeReconciliationVo != null) {
            storeReconciliationDetails.setTotalShopMoney(storeReconciliationVo.getTotalShopMoney());
            storeReconciliationDetails.setLastDeserved(storeReconciliationVo.getDeserved());
            List<OrderAmountVo> storeDetails = orderAmountMapper.getsStoreDetails(storeReconciliationVo.getCarPartsShopId(),null,null,null);
            if (storeDetails != null) {
                storeReconciliationDetails.setStoreDetails(storeDetails);
            }
        }

        return storeReconciliationDetails;
    }

    /**
     * 查询商家对账列表
     *
     * @param storeReconciliation 商家对账
     * @return 商家对账
     */
    @Override
    public List<StoreReconciliationVo> selectStoreReconciliationList(StoreReconciliation storeReconciliation)
    {
        return storeReconciliationMapper.selectStoreReconciliationList(storeReconciliation);
    }

    /**
     * 新增商家对账
     *
     * @param storeReconciliation 商家对账
     * @return 结果
     */
    @Override
    public int insertStoreReconciliation(StoreReconciliation storeReconciliation)
    {
        storeReconciliation.setCreateTime(DateUtils.getNowDate());
        return storeReconciliationMapper.insertStoreReconciliation(storeReconciliation);
    }

    /**
     * 修改商家对账
     *
     * @param storeReconciliation 商家对账
     * @return 结果
     */
    @Override
    public int updateStoreReconciliation(StoreReconciliation storeReconciliation)
    {
        storeReconciliation.setUpdateTime(DateUtils.getNowDate());
        return storeReconciliationMapper.updateStoreReconciliation(storeReconciliation);
    }

    /**
     * 批量删除商家对账
     *
     * @param storeReconciliationIds 需要删除的商家对账主键
     * @return 结果
     */
    @Override
    public int deleteStoreReconciliationByStoreReconciliationIds(Long[] storeReconciliationIds)
    {
        return storeReconciliationMapper.deleteStoreReconciliationByStoreReconciliationIds(storeReconciliationIds);
    }

    /**
     * 删除商家对账信息
     *
     * @param storeReconciliationId 商家对账主键
     * @return 结果
     */
    @Override
    public int deleteStoreReconciliationByStoreReconciliationId(Long storeReconciliationId)
    {
        return storeReconciliationMapper.deleteStoreReconciliationByStoreReconciliationId(storeReconciliationId);
    }

    @Override
    public List<StoreWithdrawalRecordVo> getRecord(Long carPartsShopId) {
        List<StoreWithdrawalRecordVo> withdrawalRecordVoList = storeWithdrawalAduitMapper.getRecord(carPartsShopId);
        return withdrawalRecordVoList;
    }

    /**
     * 我的收入
     * @param carPartsShopId
     * @return
     */
    @Override
    public StoreReconciliationDetails selectStoreReconciliationByCarPartsShopId(Long carPartsShopId) {
        StoreReconciliationDetails storeReconciliationDetails = new StoreReconciliationDetails();
        StoreReconciliationVo storeReconciliationVo = storeReconciliationMapper.selectStoreReconciliationByCarPartsShopId(carPartsShopId);
        if (storeReconciliationVo != null) {
            storeReconciliationDetails.setTotalShopMoney(storeReconciliationVo.getTotalShopMoney());
            storeReconciliationDetails.setLastDeserved(storeReconciliationVo.getDeserved());
            List<OrderAmountVo> storeDetails = orderAmountMapper.getsStoreDetails(storeReconciliationVo.getCarPartsShopId(),null,null,null);
            if (storeDetails != null) {
                storeReconciliationDetails.setStoreDetails(storeDetails);
            }
        }

        return storeReconciliationDetails;
    }
    /**
     * 获取商家对账详情-累计收入/剩余可提现
     * @param carPartsShopId
     * @return
     */
    @Override
    public StoreReconciliationDetails1 selectStoreReconciliationByCarPartsShopId1(Long carPartsShopId) {
        StoreReconciliationDetails1 storeReconciliationDetails = new StoreReconciliationDetails1();
        StoreReconciliationVo storeReconciliationVo = storeReconciliationMapper.selectStoreReconciliationByCarPartsShopId(carPartsShopId);
        if (storeReconciliationVo != null) {
            storeReconciliationDetails.setTotalShopMoney(storeReconciliationVo.getTotalShopMoney());
            storeReconciliationDetails.setLastDeserved(storeReconciliationVo.getDeserved());
        }
        return storeReconciliationDetails;
    }
    /**
     *  我的收入明细
     * @param param
     * @return
     */
    @Override
    public List<OrderAmountVo> selectStoreReconciliationByCarPartsShopId2(RevenueParam param) {
        List<OrderAmountVo> storeDetails = orderAmountMapper.getsStoreDetails(
                param.getCarPartsShopId(),param.getOrderNo(),param.getStartTime(),param.getEndTime());
        return storeDetails;
    }
    /**
     * 提现记录2
     * @param param
     * @return
     */
    @Override
    public List<StoreWithdrawalRecordVo> getRecord2(TixRecordParam param) {
        List<StoreWithdrawalRecordVo> withdrawalRecordVoList = storeWithdrawalAduitMapper.getRecord2(param);
        return withdrawalRecordVoList;
    }

}
